<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="方案名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入方案名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['hrm:socialInsncPgm:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <!--                <el-col :span="1.5">-->
                <!--                    <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['hrm:socialInsncPgm:edit']">-->
                <!--                        修改-->
                <!--                    </el-button>-->
                <!--                </el-col>-->
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['hrm:socialInsncPgm:remove']"
                    >
                        删除
                    </el-button>
                </el-col>
                <!--                <el-col :span="1.5">-->
                <!--                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['hrm:socialInsncPgm:export']">-->
                <!--                        导出-->
                <!--                    </el-button>-->
                <!--                </el-col>-->
                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="socialInsncPgmList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="方案名称" align="center" prop="name" />
                <el-table-column
                    label="公司缴纳社保"
                    align="center"
                    prop="comSocinsAmt"
                />
                <el-table-column
                    label="公司缴纳公积金"
                    align="center"
                    prop="comFundAmt"
                />
                <el-table-column
                    label="个人缴纳社保"
                    align="center"
                    prop="staffSocinsAmt"
                />
                <el-table-column
                    label="个人缴纳公积金"
                    align="center"
                    prop="staffFundAmt"
                />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['hrm:socialInsncPgm:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['hrm:socialInsncPgm:remove']"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改社保方案对话框 -->
        <el-drawer
            :title="title"
            :visible.sync="open"
            :size="1000"
            append-to-body
            class="openForm"
        >
            <el-card shadow="never" style="margin: 5px">
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12">
                            <el-form-item label="方案名称" prop="name">
                                <el-input
                                    v-model="form.name"
                                    placeholder="请输入方案名称"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12">
                            <el-form-item
                                label="公司缴纳社保"
                                prop="comSocinsAmt"
                            >
                                <el-input
                                    type="number"
                                    v-model="form.comSocinsAmt"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12">
                            <el-form-item
                                label="个人缴纳社保"
                                prop="staffSocinsAmt"
                            >
                                <el-input
                                    type="number"
                                    v-model="form.staffSocinsAmt"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12">
                            <el-form-item
                                label="公司缴纳公积金"
                                prop="comFundAmt"
                            >
                                <el-input
                                    type="number"
                                    v-model="form.comFundAmt"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12">
                            <el-form-item
                                label="个人缴纳公积金"
                                prop="staffFundAmt"
                            >
                                <el-input
                                    type="number"
                                    v-model="form.staffFundAmt"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-divider content-position="center"
                        >社保方案明细信息</el-divider
                    >
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button
                                type="primary"
                                icon="el-icon-plus"
                                size="mini"
                                @click="handleAddHrmSocialInsncPgmItem"
                            >
                                添加
                            </el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button
                                type="danger"
                                icon="el-icon-delete"
                                size="mini"
                                @click="handleDeleteHrmSocialInsncPgmItem"
                            >
                                删除
                            </el-button>
                        </el-col>
                    </el-row>
                    <el-table
                        :border="Global.tableShowBorder"
                        :data="hrmSocialInsncPgmItemList"
                        show-summary
                        :summary-method="getSummaries"
                        :row-class-name="rowHrmSocialInsncPgmItemIndex"
                        @selection-change="
                            handleHrmSocialInsncPgmItemSelectionChange
                        "
                        ref="hrmSocialInsncPgmItem"
                    >
                        <el-table-column
                            type="selection"
                            width="50"
                            align="center"
                        />
                        <el-table-column label="名称" prop="name" width="180">
                            <template slot-scope="scope">
                                <el-input
                                    v-model="scope.row.name"
                                    placeholder="请输入名称"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column label="类型" prop="type" width="180">
                            <template slot-scope="scope">
                                <el-select
                                    v-model="scope.row.type"
                                    placeholder="请选择类型"
                                >
                                    <el-option
                                        key="0"
                                        label="社保"
                                        value="0"
                                    ></el-option>
                                    <el-option
                                        key="1"
                                        label="公积金"
                                        value="1"
                                    ></el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="基数" prop="basic" width="180">
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    v-model="scope.row.basic"
                                    placeholder="请输入基数"
                                    @input="calcAmt(scope.row)"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="公司缴纳比例"
                            prop="companyRate"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    v-model="scope.row.companyRate"
                                    placeholder="请输入公司缴纳比例"
                                    @input="calcAmt(scope.row)"
                                >
                                    <template slot="append">%</template>
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="员工缴纳比例"
                            prop="staffRate"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    v-model="scope.row.staffRate"
                                    placeholder="请输入员工缴纳比例"
                                    @input="calcAmt(scope.row)"
                                >
                                    <template slot="append">%</template>
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="公司缴纳金额"
                            prop="companyAmt"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    v-model="scope.row.companyAmt"
                                    placeholder="请输入公司缴纳金额"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="员工缴纳金额"
                            prop="staffAmt"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <el-input
                                    type="number"
                                    v-model="scope.row.staffAmt"
                                    placeholder="请输入员工缴纳金额"
                                    disabled
                                >
                                    <template slot="append">元</template>
                                </el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form>
            </el-card>

            <div class="drawer-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-drawer>
    </div>
</template>

<script>
import {
    listSocialInsncPgm,
    getSocialInsncPgm,
    delSocialInsncPgm,
    addSocialInsncPgm,
    updateSocialInsncPgm,
} from "@/api/hrm/socialInsncPgm";
export default {
    name: "SocialInsncPgm",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 子表选中数据
            checkedHrmSocialInsncPgmItem: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 社保方案表格数据
            socialInsncPgmList: [],
            // 社保方案明细表格数据
            hrmSocialInsncPgmItemList: [],
            defaultItemList: [
                {
                    type: "0",
                    name: "养老保险",
                    basic: null,
                    companyRate: 16,
                    staffRate: 8,
                    companyAmt: 0,
                    staffAmt: 0,
                },
                {
                    type: "0",
                    name: "医疗保险",
                    basic: null,
                    companyRate: 9,
                    staffRate: 2,
                    companyAmt: 0,
                    staffAmt: 0,
                },
                {
                    type: "0",
                    name: "失业保险",
                    basic: null,
                    companyRate: 0.4,
                    staffRate: 0,
                    companyAmt: 0,
                    staffAmt: 0,
                },
                {
                    type: "0",
                    name: "工伤保险",
                    basic: null,
                    companyRate: 0.5,
                    staffRate: 0.5,
                    companyAmt: 0,
                    staffAmt: 0,
                },
                {
                    type: "0",
                    name: "生育保险",
                    basic: null,
                    companyRate: 0.8,
                    staffRate: 0,
                    companyAmt: 0,
                    staffAmt: 0,
                },
                {
                    type: "1",
                    name: "住房公积金",
                    basic: null,
                    companyRate: 12,
                    staffRate: 12,
                    companyAmt: 0,
                    staffAmt: 0,
                },
            ],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "createTime",
                isAsc: "descending",
                name: null,
                province: null,
                city: null,
                county: null,
                tenantId: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    {
                        required: true,
                        message: "方案名称不能为空",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询社保方案列表 */
        getList() {
            this.loading = true;
            listSocialInsncPgm(this.queryParams).then((response) => {
                this.socialInsncPgmList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                province: null,
                city: null,
                county: null,
                tenantId: null,
                createBy: null,
                createTime: null,
            };
            this.hrmSocialInsncPgmItemList = this.defaultItemList;
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加社保方案";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getSocialInsncPgm(id).then((response) => {
                this.hrmSocialInsncPgmItemList =
                    response.data.hrmSocialInsncPgmItemList;
                this.open = true;
                this.title = "修改社保方案";
                this.form = response.data;
                this.calcTotal();
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    let v = true;
                    let msg = "";
                    this.hrmSocialInsncPgmItemList.forEach((item, index) => {
                        if (!item.name) {
                            v = false;
                            msg =
                                msg +
                                "第" +
                                (index + 1) +
                                "行数据，名称不能为空。";
                        }
                        if (!item.type) {
                            v = false;
                            msg = msg + item.name + "的类型不能为空。";
                        }
                        if (!item.basic) {
                            v = false;
                            msg = msg + item.name + "的基数不能为空。";
                        }
                    });
                    if (!v) {
                        this.$modal.msgError(msg);
                        return;
                    }
                    this.form.hrmSocialInsncPgmItemList =
                        this.hrmSocialInsncPgmItemList;
                    if (this.form.id != null) {
                        updateSocialInsncPgm(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSocialInsncPgm(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除社保方案编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delSocialInsncPgm(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 社保方案明细序号 */
        rowHrmSocialInsncPgmItemIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 社保方案明细添加按钮操作 */
        handleAddHrmSocialInsncPgmItem() {
            let obj = {};
            obj.type = "";
            obj.name = "";
            obj.basic = "";
            obj.companyRate = "";
            obj.staffRate = "";
            obj.companyAmt = "";
            obj.staffAmt = "";
            this.hrmSocialInsncPgmItemList.push(obj);
        },
        /** 社保方案明细删除按钮操作 */
        handleDeleteHrmSocialInsncPgmItem() {
            if (this.checkedHrmSocialInsncPgmItem.length == 0) {
                this.$modal.msgError("请先选择要删除的社保方案明细数据");
            } else {
                const hrmSocialInsncPgmItemList =
                    this.hrmSocialInsncPgmItemList;
                const checkedHrmSocialInsncPgmItem =
                    this.checkedHrmSocialInsncPgmItem;
                this.hrmSocialInsncPgmItemList =
                    hrmSocialInsncPgmItemList.filter(function (item) {
                        return (
                            checkedHrmSocialInsncPgmItem.indexOf(item.index) ==
                            -1
                        );
                    });
            }
        },
        /** 复选框选中数据 */
        handleHrmSocialInsncPgmItemSelectionChange(selection) {
            this.checkedHrmSocialInsncPgmItem = selection.map(
                (item) => item.index
            );
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/socialInsncPgm/export",
                {
                    ...this.queryParams,
                },
                `socialInsncPgm_${new Date().getTime()}.xlsx`
            );
        },

        calcAmt(row) {
            //计算公司缴纳金额
            let camt = (row.basic ? row.basic : 0) * (row.companyRate / 100);
            row.companyAmt = camt;
            //计算个人缴纳金额
            let samt = (row.basic ? row.basic : 0) * (row.staffRate / 100);
            row.staffAmt = samt;
            this.calcTotal();
        },
        calcTotal() {
            //计算个人和公司缴纳金额
            let comSocinsAmt = 0;
            let comFundAmt = 0;
            let staffSocinsAmt = 0;
            let staffFundAmt = 0;
            this.hrmSocialInsncPgmItemList.forEach((item) => {
                //社保
                if (item.type == "0") {
                    comSocinsAmt = comSocinsAmt + item.companyAmt;
                    staffSocinsAmt = staffSocinsAmt + item.staffAmt;
                } else if (item.type == "1") {
                    //公积金
                    comFundAmt = comFundAmt + item.companyAmt;
                    staffFundAmt = staffFundAmt + item.staffAmt;
                }
            });
            this.form.comSocinsAmt = comSocinsAmt.toFixed(2);
            this.form.comFundAmt = comFundAmt.toFixed(2);
            this.form.staffSocinsAmt = staffSocinsAmt.toFixed(2);
            this.form.staffFundAmt = staffFundAmt.toFixed(2);

            console.log("计算完毕：" + JSON.stringify(this.form));
            let f = JSON.stringify(this.form);
            this.form = JSON.parse(f);
        },
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计";
                    return;
                }
                const values = data.map((item) =>
                    Number(item[column.property])
                );
                if (
                    !values.every((value) => isNaN(value)) &&
                    index != 2 &&
                    index != 3 &&
                    index != 4
                ) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return (Number(prev) + Number(curr)).toFixed(2);
                        } else {
                            return prev.toFixed(2);
                        }
                    }, 0);
                    sums[index] += " 元";
                } else {
                    sums[index] = "";
                }
            });
            console.log(sums);
            return sums;
        },
    },
};
</script>
